<template>
  <div>
    <!-- 头部 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon name="cross" size="0.37333334rem" color="white" @click="$emit('closePop')"/>
      </template>
    </van-nav-bar>
    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span>我的频道 {{show ? "点击移出频道":"点击进入频道 "}}</span>
        <span @click="show=!show">{{show ? "完成 ":"编辑"}}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex" >
        <van-col span="6" v-for="obj in userChannelList" :key="obj.id" @click="removeFn(obj)">
          <div class="channel-item van-hairline--surround">
            {{obj.name}}
            <!-- 删除的徽标 -->
            <van-badge color="transparent" class="cross-badge">
              <template #content>
                <van-icon
                v-if="show===true && obj.id!==0"
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="0.32rem"

                />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="obj in unChinnelList" :key="obj.id" @click="addFn(obj)">
          <div class="channel-item van-hairline--surround">{{obj.name}}</div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import { allChannelListAPI, userChannelListAPI } from '@/api'
export default {
  props: {
    userChannelList: {
      type: Array,
      default: () => [] // 默认是空数组
    }
  },
  data () {
    return {
      show: false, // 是否显示所有徽记
      allChannelList: []
    }
  },
  methods: {
    // 添加对象中
    addFn (obj) {
      // 把对象添加到响应的列表中
      if (this.show === true && obj.name !== '推荐') {
        this.userChannelList.push(obj)
        this.updateChannelList()
      }
    },
    // 数据更新
    async updateChannelList () {
      const arr = this.deepCopy(this.userChannelList)
      const index = arr.findIndex(item => {
        return item.name === '推荐'
      })
      arr.splice(index, 1)
      arr.forEach((item, index) => {
        delete item.name
        item.seq = index + 1
      })

      const res = await userChannelListAPI({
        channels: arr
      })
      console.log(res)
    },
    // 深拷贝
    deepCopy (arr) {
      const newArr = JSON.stringify(arr)
      return JSON.parse(newArr)
    },
    // 删除频道 或者直接跳转到当前所选的频道
    removeFn (obj) {
      // 获取删除对象在列表中对应的索引
      if (this.show === true && obj.name !== '推荐') {
        const index = this.userChannelList.findIndex(item => item.id === obj.id)
        this.userChannelList.splice(index, 1)
        this.updateChannelList()
      } else {
        // 当点击我的频道中的任何一个频道时,关闭当前popup弹窗
        this.$emit('closePop')
        // 传递到前台的 articleClickFn 切换频道的方法
        this.$emit('ccid', obj)
      }
    }
  },
  computed: {
    // 筛选出未选择的频道 ,因为是通过两个已知变量计算得来 所以写这
    unChinnelList () {
      // 删选出符合条件的 return false 不返回 return true 返回
      return this.allChannelList.filter(obj => {
        const AIndex = this.userChannelList.findIndex(item => {
          return item.id === obj.id
        })
        if (AIndex !== -1) {
          return false
        } else {
          return true
        }
      })
    }
  },
  async created () {
    const res = await allChannelListAPI()
    // console.log(res)
    this.allChannelList = res.data.data.channels
    console.log(this.allChannelList)
  }
}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small_title {
  font-size: 12px;
  color: gray;
}
</style>
